package com.smart.gaorenhui.task;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;

@Log4j2
public class Meizhuang {

    static String URL_TPL = "https://wx.kokoxxmx.cn/dev-api/course/student/page";
    static String token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImM3NzE4OWQ2LTMyYzAtNDVkYi05ZTRkLWJhN2Q1Y2E3NDRhMSJ9.h2Jz3LZOf9DIP-Hcb9vK0fsui8dTU8uojF18HMCZriVi4Ia3T_x05i6FMnAXHTPzNRBucBLRXtRKKJFeLCKpkg";
    static String body = "{\"pageNum\":%s,\"pageSize\":%s,\"serialNum\":null,\"durationNum\":null,\"studentName\":null," +
            "\"studentTel\":null,\"groupId\":null,\"unionid\":null,\"userid\":null,\"courseName\":null," +
            "\"coursePeriodList\":null,\"coursePeriodId\":%s,\"courseId\":6,\"classNumList\":[],\"page\":{\"pageNum\":%s,\"pageSize\":%s}}";


    // 获取请求参数
    public static String getBody(int pageNum, int pageSize, int coursePeriodId) {
        return String.format(body, pageNum, pageNum, coursePeriodId, pageNum, pageSize);
    }

    public static void main(String[] args) {
        //13 ,28
        log.error("--------------开始采集数据-------------");
        dxc();

        log.error("--------------数据采集完毕-------------");
    }


    public static void dxc() {
        //28
        ArrayList<MeizhuangBody> excleList = new ArrayList<>();
        for (int i = 13; i <= 28; i++) {


            System.out.println("正在获取第" + i + "期的总数据");
            String params = getBody(1, 1, i);
            String result = HttpUtil.createRequest(Method.POST, URL_TPL)
                    .header("Authorization", token)
                    .body(params)
                    .execute()
                    .body();
            int total = JSONObject.parseObject(result).getIntValue("total");
            if (total > 0) {
                getList(excleList, i, total);
            }


        }
        String rq = DateUtil.format(new Date(), "yy-MM-dd-hh-mm");
        String fileName = "d:/美装/".concat(rq).concat(".xlsx");
        BigExcelWriter bigWriter = ExcelUtil.getBigWriter(fileName);
        bigWriter.write(excleList, true);
        bigWriter.close();
    }

    public static void getList(ArrayList<MeizhuangBody> excleList, int qh, int total) {
        try {
            int page = 1;
            log.warn(" 采集第{}期的数据。共{}页,正在采集第{}页数据,每页数据{}条,", qh, total, page, total);
            String params = getBody(page, total, qh);
            String result = HttpUtil.createRequest(Method.POST, URL_TPL)
                    .header("Authorization", token)
                    .body(params)
                    .setReadTimeout(1000 * 600)
                    .execute()
                    .body();


            JSONArray rows = JSONObject.parseObject(result).getJSONArray("rows");
            log.warn("原始数据采集完成 本次拿到{}条数据", rows.size());
            for (Object row : rows) {
                MeizhuangBody meizhuangBody = JSONObject.parseObject(JSONObject.toJSONString(row), MeizhuangBody.class);
                meizhuangBody.setQh(String.valueOf(qh));
                excleList.add(meizhuangBody);
                log.warn("-------------采集第{}期的数据采集完毕----------", qh);
            }
        } catch (Exception e) {
            log.warn("数据写入失败：" + e.getMessage());
        }
    }
}
